﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;

namespace mal.WP7.GoogleAnalytics.Business.Tiles
{
    public partial class WebsiteTile : UserControl
    {
        #region Fields
        public static readonly DependencyProperty NumberOfVisitorsProperty
         = DependencyProperty.Register(
                                   "NumberOfVisitors", typeof(double),
                                   typeof(WebsiteTile),
                                   new PropertyMetadata(double.MinValue, NumberOfVisitorsChanged)
                                 );

        public static readonly DependencyProperty DeltaProperty
         = DependencyProperty.Register(
                                   "Delta", typeof(double?),
                                   typeof(WebsiteTile),
                                   new PropertyMetadata(null, DeltaChanged)
                                 );

        #endregion

        #region Construction / Destruction
        public WebsiteTile()
        {
            InitializeComponent();

            this.Arrow.Visibility = Visibility.Collapsed;
            this.delta.Text = string.Empty;
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public double NumberOfVisitors
        {
            get
            {
                return (double)GetValue(NumberOfVisitorsProperty);
            }

            set
            {
                SetValue(NumberOfVisitorsProperty, value);
            }
        }

        public double Delta
        {
            get
            {
                return (double)GetValue(DeltaProperty);
            }

            set
            {
                SetValue(DeltaProperty, value);
            }
        }
        #endregion

        #region Methods
        private static void NumberOfVisitorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((WebsiteTile)d).SetNumberOfVisitors((double)e.NewValue);
        }

        private void SetNumberOfVisitors(double value)
        {
            this.visitorsCount.Text = value.ToString();
        }

        private static void DeltaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((WebsiteTile)d).SetDelta((double?)e.NewValue);
        }

        private void SetDelta(double? value)
        {
            if (value.HasValue)
            {
                this.Arrow.Visibility = Visibility.Visible;
                string format = "{0:F1}%";
                if (Math.Abs(value.Value) > 100 || value.Value == 0)
                {
                    format = "{0:F0}%";
                }

                if (value > 0)
                {
                    this.Arrow.Source = new BitmapImage(new Uri("/Images/stats-up.png", UriKind.RelativeOrAbsolute));
                    this.delta.Text = string.Format("+" + format, value);
                }
                else if (value == 0)
                {
                    this.Arrow.Visibility = Visibility.Collapsed;
                    this.delta.Text = string.Format(format, value);
                }
                else if (value < 0)
                {
                    this.Arrow.Source = new BitmapImage(new Uri("/Images/stats-down.png", UriKind.RelativeOrAbsolute));
                    this.delta.Text = string.Format(format, value);
                }
            }
            else
            {
                this.Arrow.Visibility = Visibility.Collapsed;
                this.delta.Text = string.Empty;
            }
        }
        #endregion

        #region Events

        #endregion
    }
}
